'''
Created on 10 Nov 2010

@author: tiberious
'''
# test mkv reader
# (c) tiberious.org 2010

import sys
from matroskaTagger import MatroskaTagReaderWriter

class test:
    
    def __init__(self):
        self.mkv = None
        self.tags = {}
        
    def __del__(self):
        del self.mkv
        
    def load(self, location):
        self.mkv = MatroskaTagWriter(location)
        
    def getTags(self):
        self.tags = self.mkv.getTags()
        return self.tags
    
    def getInfo(self):
        return self.mkv.getInfo()
    
    def saveTags(self, tags = None):
        if tags != None:
            self.tags = tags
        
        self.mkv.setTags(self.tags)
        self.mkv.writeTags()
        
    def dumpTags(self):
        from pprint import pprint
        if len(self.tags) == 0:
            self.getTags()
        info = self.getInfo()
        
        if info != False:
            length = info['Duration'][0] * info['TimecodeScale'][0] / 1e9
            print "Length = %f seconds" % length
            
        if self.tags != False:
            pprint(self.tags)
        else:
            print 'No Tags Found in %s' % location        

if __name__ == '__main__':
    # get tags
    location = sys.argv[1]
    atest = test()
    atest.load(location)
    atest.dumpTags()
    
    try:
        l2 = sys.argv[2]
        awrite = test()
        awrite.load(l2)
        awrite.saveTags(atest.tags)
    except:
        print 'Specify second file to write to'